#include "StdAfx.h"
#include "CustomDialog.h"

#define TITLE_HEIGHT 35

IMPLEMENT_DYNAMIC(CCustomDialog, CDialog)

CCustomDialog::CCustomDialog(UINT nIDTemplate, CWnd* pParent)
	: CDialog(nIDTemplate, pParent)
{
	m_bHasDraged = false;
}

CCustomDialog::~CCustomDialog(void)
{
}

BEGIN_MESSAGE_MAP(CCustomDialog, CDialog)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

void CCustomDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if (point.y < 0 || point.y > TITLE_HEIGHT)
		return;

	m_bHasDraged = TRUE;
	m_pointDown = point;
	SetCapture();

	CDialog::OnLButtonDown(nFlags, point);
}

void CCustomDialog::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default

	if (!m_bHasDraged)
		return;
	
	m_bHasDraged = false;
	ReleaseCapture();

	CDialog::OnLButtonUp(nFlags, point);
}

void CCustomDialog::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if (!m_bHasDraged)
		return;

	int offleft = point.x - m_pointDown.x;
	int offtop = point.y - m_pointDown.y;

	CRect wrc;
	GetWindowRect(&wrc);

	wrc.OffsetRect(offleft, offtop);
	MoveWindow(&wrc, TRUE );

	CDialog::OnMouseMove(nFlags, point);
}
